var teamId;
var tmGameId;
var teamGame={};
$(document).ready(function(){
	$.ajaxSettings.async = false;
	$('#header').animate({'top':'0px'},400);
	teamId = args.teamId; //取得id的值
	tmGameId = args.tmGameId;
	if(tmGameId!=undefined){
		$.getJSON(path+"/soccer/teamGameAction!findGame.action",{tmGameId: tmGameId}, function(data){
			teamGame=data.tmTeamGame;
		});
	}else{
		teamGame=JSON.parse(sessionStorage.getItem("teamGameEdit"));
		sessionStorage.clear();
	}
	if(teamGame!=null){
		$("#rowId").val(teamGame.rowId);
		$("#gameName").val(teamGame.gameName);
		$("#gameType").val(teamGame.gameType);
		$("#scoredFlag").val(teamGame.scoredFlag);
		$("#gameDate").val(moment(teamGame.gameDate,"YYYY-MM-DD").format("YYYY-MM-DD"));
		$("#startTime").val(moment(teamGame.startTime,"YYYY-MM-DD HH:mm:ss").format("HH:mm"));
		$("#endTime").val(moment(teamGame.endTime,"YYYY-MM-DD HH:mm:ss").format("HH:mm"));
		$("#arriveTime").val(moment(teamGame.arriveTime,"YYYY-MM-DD HH:mm:ss").format("HH:mm"));
		$("#punishTime").val(moment(teamGame.punishTime,"YYYY-MM-DD HH:mm:ss").format("HH:mm"));
		$("#signupEndDate").val(moment(teamGame.signupEndTime,"YYYY-MM-DD HH:mm:ss").format("YYYY-MM-DD"));
		$("#signupEndTime").val(moment(teamGame.signupEndTime,"YYYY-MM-DD HH:mm:ss").format("HH:mm"));
	}
	//初始化球场选择
	$.getJSON(path+"/soccer/teamGameAction!selectPlaceByTeamId.action",{teamId: teamId}, function(data){
		var placeList = data.placeList;
		var placeSelObj = $("#placeId");
		if(teamGame==null||teamGame.placeId=="-1"){
			placeSelObj.append("<option value='-1' selected='true'>待定</option>");
		}else{
			placeSelObj.append("<option value='-1'>待定</option>");
		}
		$.each(placeList,function(index,obj){
			if(teamGame!=null&&teamGame.placeId==obj.rowId){
				placeSelObj.append("<option value='"+obj.rowId+"' selected='true'>"+obj.placeName+"</option>");
			}else{
				placeSelObj.append("<option value='"+obj.rowId+"'>"+obj.placeName+"</option>");
			}
		});
	});
	
	//初始化对手选择
	$.getJSON(path+"/soccer/teamAction!selectTeamOpponent.action",{teamId:teamId}, function(data){
		var opponentList = data.opponentList;
		var opponentTeamSelObj = $("#opponentTeamId");
		if(teamGame==null || teamGame.opponentTeamId=="-1"){
			opponentTeamSelObj.append("<option value='-1' selected='true'>无</option>");
		}else{
			opponentTeamSelObj.append("<option value='-1'>无</option>");
		}		
		$.each(opponentList,function(index,obj){
			if(teamGame!=null&&teamGame.opponentTeamId==obj.rowId){
				opponentTeamSelObj.append("<option value='"+obj.rowId+"' selected='true'>"+obj.opponentName+"</option>");
			}else{
				opponentTeamSelObj.append("<option value='"+obj.rowId+"'>"+obj.opponentName+"</option>");
			}
		});
	});
	$("#submitBtn").click(function(){
		var strMessage="";
		//检查必填
		if($("input[id='gameName']").val()==undefined||$.trim($("input[id='gameName']").val())==""){
			strMessage += "比赛名称必填!";
			alert(strMessage);
			return false;
		}
		if($("#gameDate").val()==undefined||$.trim($("#gameDate").val())==""){
			strMessage += "比赛日期必填!";
			alert(strMessage);
			return false;
		}
		if($("#startTime").val()==undefined||$.trim($("#startTime").val())==""){
			strMessage += "开始时间必填!";
			alert(strMessage);
			return false;
		}
		if($("#endTime").val()==undefined||$.trim($("#endTime").val())==""){
			strMessage += "结束时间必填!";
			alert(strMessage);
			return false;
		}
		if($("#arriveTime").val()==undefined||$.trim($("#arriveTime").val())==""){
			strMessage += "到场时间必填!";
			alert(strMessage);
			return false;
		}
		if($("#punishTime").val()==undefined||$.trim($("#punishTime").val())==""){
			strMessage += "迟到处罚时间必填!";
			alert(strMessage);
			return false;
		}
		if($("#signupEndDate").val()==undefined||$.trim($("#signupEndDate").val())==""||$("#signupEndTime").val()==undefined||$.trim($("#signupEndTime").val())==""){
			strMessage += "报名截止时间必填!";
			alert(strMessage);
			return false;
		}
		//检查必填结束
		teamGameInfo();
		var jsonStr = JSON.stringify(teamGame);
		var postData={jsonStr:jsonStr};
		$.post(path+"/soccer/teamGameAction!saveTeamGame.action",postData, function(data){
//			strMessage = data.strMessage;
//			$('#strMessage').text(strMessage);
			window.location.href=path+"/soccer/teamGame.jsp?teamId="+teamId;
		},'json');
	});
});
function teamGameInfo(){
	if(teamGame==null){
		teamGame = {};
	}
	teamGame.teamId=teamId;
	teamGame.gameSource="9";
	teamGame.gameName=$("input[id='gameName']").val();
	teamGame.gameType=$("#gameType option:selected").val();
	teamGame.scoredFlag=$("#scoredFlag option:selected").val();
	teamGame.gameDate=$("#gameDate").val();
	teamGame.startTime=$("#startTime").val();
	teamGame.endTime=$("#endTime").val();
	teamGame.arriveTime=$("#arriveTime").val();
	teamGame.punishTime=$("#punishTime").val();
	teamGame.signupEndTime=$("#signupEndDate").val()+" "+$("#signupEndTime").val();
	teamGame.placeId=$("#placeId").val();
	teamGame.placeName=$("#placeId option:selected").text();
	teamGame.opponentTeamId=$("#opponentTeamId option:selected").val();
	teamGame.opponentName=$("#opponentTeamId option:selected").text();
}
function newPlace(){
	//缓存数据
	teamGameInfo();
	sessionStorage.setItem("teamGameEdit", JSON.stringify(teamGame));
	var timestamp = (new Date()).valueOf();
	var backURL="/soccer/teamGameEdit.jsp";
	window.location.href=path+"/soccer/mdm/placeEdit.jsp?teamId="+teamId+"&backURL="+backURL+"&timestamp="+timestamp;
}
function newOpponent(){
	//缓存数据
	teamGameInfo();
	sessionStorage.setItem("teamGameEdit", JSON.stringify(teamGame));
	var timestamp = (new Date()).valueOf();
	var backURL="/soccer/teamGameEdit.jsp";
	window.location.href=path+"/soccer/mdm/opponentEdit.jsp?teamId="+teamId+"&backURL="+backURL+"&timestamp="+timestamp;
}
function goBack(){
	window.location.href=path+"/soccer/teamGame.jsp?teamId="+teamId;
}
